/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.ui.widgets;

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Graphics;

/**
 * Progress bar widget
 * <p>
 * The Dimensions of the progress bar is thickness =5px and height=50. This
 * value can be changed using {@link ProgressBar#setBarDimensions(int, int)}
 * 
 * </p>
 * 
 * @author kentelt
 * 
 */
public class ProgressBar extends Widget {

	private final class Ani extends TimerTask {
		public final void run() {
			while (barLength < aniBarLen) {
				barLength++;
				reDrawWidget();
				try {
					Thread.sleep(20);
				} catch (InterruptedException e) {
				}
			}

		}

	}

	private int aniBarLen;
	private int barHeight = 90;
	/*
	 * the width of the progress
	 */
	private int barLength;
	private int barThick = 5;

	private short layout = LAYOUT_HORIZONTAL;

	private int percent;

	private boolean useAni;

	public ProgressBar(Theme theme) {
		super(theme, 0, 0);
		updateModel();
	}

	public void cleanUp() {

	}

	public final int getPercent() {
		return percent;

	}

	public void render(Graphics g) {
		if (!isVisible)
			return;
		capture(g, width, height, true);
		g.setColor(theme.fgColor);
		if (layout == LAYOUT_HORIZONTAL)
			g.fillRect(0, 0, barLength, barThick);
		else
			g.fillRect(0, height - barLength, barThick, barLength);
		release(g, width, height, true);

	}

	/**
	 * Enable/Disable animation of progress bar when the progress value changes.
	 * 
	 * @param canAnimate
	 *            Default is false
	 */
	public void setAnimation(boolean canAnimate) {
		this.useAni = canAnimate;
	}

	/**
	 * Set the thickness of the progress bar
	 * 
	 * @param thickness
	 *            of the bar
	 * @param length
	 *            of the progress bar
	 */
	public void setBarDimensions(int thickness, int length) {
		this.barThick = thickness;
		this.barHeight = length;
		updateModel();
		setPercent(percent);
		reDraw();
	}

	public void setLayout(short layout) {
		this.layout = layout;
		updateModel();
		setPercent(percent);
		reDraw();

		stateChanged(this);
	}

	/**
	 * 
	 * @param percent
	 * @throws IllegalArgumentException
	 *             if the percentage value is < 0 or >100
	 */
	public void setPercent(int percent) {
		final int prev = barLength;
		if (percent > 100 || percent < 0) {
			throw new IllegalArgumentException("Invalid percent");
		}
		this.percent = percent;
		if (layout == LAYOUT_HORIZONTAL)
			barLength = width * percent / 100;
		else {
			barLength = height * percent / 100;
		}
		/* Animate the change in progress */
		if (useAni) {
			System.out.println("can use ani");
			aniBarLen = barLength;
			barLength = prev;
			final Timer timer = new Timer();
			timer.schedule(new Ani(), 0);
		}
		reDrawWidget();
		stateChanged(this);
	}

	protected void updateModel() {
		if (layout == LAYOUT_HORIZONTAL) {
			height = barThick;
			width = barHeight;
		} else {
			width = barThick;
			height = barHeight;
		}
		sizeChanged(width, height);
	}
}
